﻿#pragma once

struct _stRGB
{
	BYTE blue;
	BYTE green;
	BYTE red;
	
	_stRGB() : blue(0), green(0), red(0) {}
	_stRGB(BYTE b, BYTE g, BYTE r) : blue(b), green(g), red(r) {}

	DWORD Trans2DWORD() const { return ( red << 16 ) + ( green << 8 ) + blue; }
	bool operator == (const _stRGB& st) const
	{
		return red == st.red && green == st.green && blue == st.blue;
	}
};

struct _stRGBHash
{
	std::size_t operator()(const _stRGB& k) const
	{
		std::hash<DWORD> HASH;
		return HASH(k.Trans2DWORD());
	}
};

class CTimeElapsed
{
public:
	CTimeElapsed() : m_begin(std::chrono::high_resolution_clock::now()) {}
	void reset() { m_begin = std::chrono::high_resolution_clock::now(); }

	//默认输出毫秒
	int64_t elapsed() const { return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - m_begin).count(); }
	//微秒
	int64_t elapsed_micro() const{ return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - m_begin).count(); }
	//纳秒
	int64_t elapsed_nano() const{ return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - m_begin).count(); }
	//秒
	int64_t elapsed_seconds() const{ return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - m_begin).count(); }
	//分
	int64_t elapsed_minutes() const{ return std::chrono::duration_cast<std::chrono::minutes>(std::chrono::high_resolution_clock::now() - m_begin).count(); }
	//时
	int64_t elapsed_hours() const{ return std::chrono::duration_cast<std::chrono::hours>(std::chrono::high_resolution_clock::now() - m_begin).count(); }

private:
	std::chrono::time_point<std::chrono::high_resolution_clock> m_begin;
};

struct _stDate
{
private:
	int year;	// 负号-代表公元前
	BYTE month;	
	BYTE day;
public:
	_stDate() : year(0), month(1), day(1) {}
	_stDate(short y, BYTE m, BYTE d) : year(y), month(m), day(d) {}
	bool operator < (const _stDate& ti) const;


	bool SetYMDbyWstr(std::wstring strDate);		// "848.1.1"的输入
	short GetYear() const { return year; }
	BYTE GetMonth() const { return month; }
	BYTE GetDay() const { return day; }

	int GetMinusMonth(_stDate& date) const { return (year - date.year) * 12 + month - date.month; }	// 此月到彼月的总月份
	int GetMinusYear(_stDate& date) const { return year - date.year; }	// 此年到彼年的年份

	_stDate& operator ++ ()
	{
		++day;
		Standardize();
		return *this;
	}
private:
	void Standardize();
	bool IsLeepYear() const;
};

class AnyData
{
public:
	template<typename RealDataType>
	AnyData(RealDataType* data) : m_pRealData(data){}

	template<typename RealDataType>
	RealDataType& GetRealDataType() {
		CHECK_RETURN(m_pRealData);
		return *reinterpret_cast<RealDataType*>(m_pRealData);
	}

	template<typename RealDataType>
	const RealDataType& GetRealDataType() const {
		CHECK_RETURN(m_pRealData);
		return *reinterpret_cast<const RealDataType*>(m_pRealData);
	}

	template<typename RealDataType>
	AnyData& operator =(RealDataType* _Data)
	{
		Set<RealDataType>(_Data);
		return *this;
	}

private:
	template<typename RealDataType>
	void Set(RealDataType* _Data)
	{
		m_pRealData = reinterpret_cast<void*>(_Data);
	}

private:
	void* m_pRealData;
};

template <typename _TData>
struct _TNode
{
	std::set<_TNode*> m_ParrentList;
	_TData tData;
	std::set<_TNode*> m_ChildList;

	_TNode(_TData& t) : tData(t) {}
	void InsertParrentNode(const _TNode<_TData>* tNode, bool bOperateParrent);
	void InsertChildNode(const _TNode<_TData>* tNode, bool bOperateChild);
	void EraseParrentNode(const _TNode<_TData>* tNode, bool bOperateParrent);	// 小心内存泄露 可引入智能指针 zwb_promising
	void EraseChildNode(const _TNode<_TData>* tNode, bool bOperateChild);
};

template <typename _TData>
class _DoublyTree
{
public:
	_TNode m_TreeHead;
};

template <typename _TData>
class _TreeIterator
{
typedef typename std::list<_TNode<_TData>*>::iterator ListIterator;
friend class _DoublyTree<_TData>;

private:
	_DoublyTree<_TData>* _tree;						//Tree data
	ListIterator _lit;								//List Iterator
public:
	_TreeIterator(){}								//默认构造函数
 	_TreeIterator(const _TreeIterator& it);			//复制构造函数
// 	_TreeIterator(_DoublyTree* t, _TNode<_TData>* node);	//构造函数
// 	_TreeIterator(_DoublyTree* t, ListIterator it);		//构造函数
	//运算符重载
	void operator =(const _TreeIterator& it);		//赋值运算符重载
	bool operator ==(const _TreeIterator& it);		//关系运算符重载
	bool operator !=(const _TreeIterator& it);		//关系运算符重载
	_TreeIterator<_TData>& operator ++();					//前缀++运算符
	_TreeIterator<_TData> operator ++(int);					//后缀++运算符
	_TData& operator *() const;						//获得节点信息
	bool operator !();								//赋值运算符重载
};